package cn.edu.lingnan.fruitshop.controller;


import cn.edu.lingnan.fruitshop.common.JSONResult;
import cn.edu.lingnan.fruitshop.pojo.Goodstable;
import cn.edu.lingnan.fruitshop.service.GoodstableService;
import org.springframework.beans.factory.annotation.Autowired;
import cn.edu.lingnan.fruitshop.service.GoodstableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 张小凡
 * @since 2022-01-04
 */
@Controller
@RequestMapping("/goodstable")
public class GoodstableController {


    @Autowired
    private GoodstableService goodstableService;

    //    商品详情
    @GetMapping("/goodsDetail")
    public String goodDetail(Integer goodId, Model model,HttpSession session) {
        return goodstableService.goodDetail(goodId, model,session);
    }


    //从商品详情页面按立即购买跳转到结算界面
    @GetMapping("/settlement")
    public String settlement(Integer goodId, Model model, Integer num,HttpSession session,Integer orderId,Integer addressStatus) {

        return goodstableService.settlement(goodId, model, num,session,orderId,addressStatus);
    }

    /**
     * 搜索
     *
     * @param model
     * @param goodsName
     * @return
     */
    @GetMapping("/searchForGoods")
    public String searchForGoods(Model model, String goodsName) {
        return goodstableService.searchForGoods(model, goodsName);
    }


    //商品图片异步上传
    @RequestMapping("/picture")
    @ResponseBody
    public JSONResult<Void> picture(MultipartFile file) {
        return goodstableService.picture(file);
    }

    //获取商品图片url
    @GetMapping("/getPrictureUrl")
    @ResponseBody
    public JSONResult<Void> getPictureUrl() {
        return goodstableService.getPictureUrl();
    }


    // 检查商品名称
    @GetMapping("/checkName")
    @ResponseBody
    public JSONResult<Void> checkName(String goodName) {
        return goodstableService.checkName(goodName);
    }

    //检查商品详细
    @GetMapping("/checkGoodDetail")
    @ResponseBody
    public JSONResult<Void> checkGoodDetail(String goodDetail) {
        return goodstableService.checkGoodDetail(goodDetail);
    }

    // 检查商品数量
    @GetMapping("/checkGoodNum")
    @ResponseBody
    public JSONResult<Void> checkGoodNum(String goodNum) {
        return goodstableService.checkGoodNum(goodNum);
    }

    // 检查商品价格
    @GetMapping("/checkGoodPrice")
    @ResponseBody
    public JSONResult<Void> checkGoodPrice(String goodPrice) {
        return goodstableService.checkGoodPrice(goodPrice);
    }

    //添加商品
    @PostMapping("/addGoods")
    @ResponseBody
    public JSONResult<Void> addGoods(String name, String newPrice,
                                     String store, String goodDetail, Integer typeId, HttpSession session) {
        return goodstableService.addGoods(name, newPrice, store, goodDetail, typeId,session);
    }

    //跳到更新商品页面
    @GetMapping("/toUpdateGoodIndex")
    public String toUpdateGoodIndex(Integer id, Model model,Integer pageNum,HttpSession session) {
        return goodstableService.toUpdateGoodIndex(id, model,pageNum,session);
    }

    @PostMapping("/updateGood")
    @ResponseBody
    public JSONResult<Void> updateGood(String name, String newPrice,
                                       String store, String goodDetail, Integer typeId, Integer id,HttpSession session) {
        return goodstableService.updateGood(name, newPrice, store, goodDetail, typeId, id,session);
    }

    @GetMapping("/deleteGoodById")
    public String deleteGoodById(Integer id,Integer pageNum) {
        return goodstableService.deleteGoodById(id,pageNum);
    }

    //批量删除商品
    @GetMapping("/batchDeleteGood")
    public String batchDeleteGood(String goodIds) {
        return goodstableService.batchDeleteGood(goodIds);
    }
}




